package com.dongecloud.consumer;

import com.donge.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    DiscoveryClient client;
    int count = 0;
    @GetMapping("/hello")
    public String getHello(){
        HttpURLConnection con = null;
        List<ServiceInstance> instances = client.getInstances("provider");
        ServiceInstance serviceInstance = instances.get((count++) % instances.size());
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        StringBuilder sb = new StringBuilder();
        sb.append("http://")
                .append(host)
                .append(":")
                .append(port)
                .append("/hello");
        try {
            URL url = new URL(sb.toString());
            con = (HttpURLConnection) url.openConnection();
            if (con.getResponseCode() == 200){
                BufferedReader read = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = read.readLine();
                read.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return "hello";
    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello1")
    public String getHello1(){
        return restTemplate.getForObject("http://provider/hello",String.class);
    }

    //get请求方式
    @GetMapping("/getHello")
    public String getHello2(){
        String donge = restTemplate.getForObject("http://provider/hello1?name={1}", String.class, "donge");
        Map map = new HashMap<>();
        map.put("name","东哥");
        String restMap = restTemplate.getForObject("http://provider/hello1?name={name}", String.class, map);
        String url = null;
        String ergouzi = null;
        try {
            url = "http://provider/hello1?name=" + URLEncoder.encode("二狗子","UTF-8");
            URI uri = URI.create(url);
            ergouzi = restTemplate.getForObject(uri, String.class);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return donge + restMap + ergouzi;
    }

    //post请求方式
    @GetMapping("/postUser")
    public void postUser(){
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("username","东哥");
        map.add("password","123456");
        map.add("id",99);
        User keyValueUser = restTemplate.postForObject("http://provider/user1", map, User.class);
        System.out.println("keyValueUser:"+keyValueUser);

        keyValueUser.setId(98);
        User JsonUser = restTemplate.postForObject("http://provider/user2", keyValueUser, User.class);
        System.out.println("JsonUser:"+JsonUser);
    }
    
    @GetMapping("/postLocation")
    public void postLocation(){
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("username","东哥");
        map.add("password","123456");
        map.add("id",99);
        URI uri = restTemplate.postForLocation("http://provider/register", map);
        String postLocation = restTemplate.getForObject(uri, String.class);
        System.out.println("postLocation:"+postLocation);
    }
    //put情趣方式
    @GetMapping("/putUser")
    public void putUser(){
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("username","donge");
        map.add("password","123456");
        map.add("id",99);
        restTemplate.put("http://provider/user1", map, User.class);
        User user = new User();
        user.setId(98);
        user.setUsername("ergouzi");
        user.setPassword("654321");
        restTemplate.put("http://provider/user2", user, User.class);
    }

    //delete方式
    @GetMapping("/deleteUser")
    public void deleteUser(){
        restTemplate.delete("http://provider/user1?id={1}", 99);
        restTemplate.put("http://provider/user2/{id}", 98);
    }
}
